package com.hl.web.fore;

import com.hl.pojo.Question;
import com.hl.service.QuestionService;
import com.hl.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/question")
public class QuestionController {
    @Autowired
    private QuestionService questionService;
    @GetMapping("/findAll1")
    public ResultEntity findAll(){
        return ResultEntity.ok(questionService.findAll()) ;
    }
    @GetMapping("/findAll2")
    public ResultEntity findAll1(){
        List<Question> list = questionService.findAll1();
        list.sort(new Comparator<Question>() {
            @Override
            public int compare(Question o1, Question o2) {
                if(o1.getReplyDate() == null){
                    return 1;
                }else if(o2.getReplyDate() == null){
                    return -1;
                }
                return o2.getReplyDate().compareTo(o1.getReplyDate());
            }
        });
        return ResultEntity.ok(list) ;
    }

    @GetMapping("/findAll3")
    public ResultEntity findAll2(){
        List<Question> list = questionService.findAll();
        list.sort((o1, o2) -> o2.getSum()-o1.getSum());
        return ResultEntity.ok(list) ;
    }
    @GetMapping("/findAll4")
    public ResultEntity findAll4(){
        return ResultEntity.ok(questionService.findAll4()) ;
    }
    @GetMapping("/findById")
    public ResultEntity findById(Integer id){
        return ResultEntity.ok(questionService.findById(id)) ;
    }
    @GetMapping("/delete")
    public ResultEntity delete(Integer id){
         questionService.delete(id);
         return ResultEntity.ok() ;
    }
    @PostMapping("/add")
    public ResultEntity add(@RequestBody Question question){
        Date date = new Date();
        SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        question.setTime(s.format(date));
         questionService.add(question);
         return ResultEntity.ok() ;
    }
    @PostMapping("/update")
    public ResultEntity update(Question question){
         questionService.update(question);
         return ResultEntity.ok() ;
    }

    @GetMapping("/findKeCheng")
    public ResultEntity findKeCheng(Integer id){
        return ResultEntity.ok(questionService.findkecheng(id));
    }

    @GetMapping("/sosuo")
    public ResultEntity sosuo(String name){
        return ResultEntity.ok(questionService.sosuo(name));
    }
}
